namespace TaskProject.Extension.Plan
{
    /// <summary>
    /// 生产计划DTO
    /// </summary>
    public class ProductionPlanDTO
    {
        /// <summary>
        /// 生产计划ID
        /// </summary>
        public int PlanId { get; set; }

        /// <summary>
        /// 计划主题
        /// </summary>
        public string PlanSubject { get; set; }

        /// <summary>
        /// 计划单号
        /// </summary>
        public string PlanOrderNumber { get; set; }

        /// <summary>
        /// 单据日期
        /// </summary>
        public DateTime? DocumentDate { get; set; }

        /// <summary>
        /// 备注
        /// </summary>
        public string? Remarks { get; set; }

        /// <summary>
        /// 销售订单号
        /// </summary>
        public string? SalesOrderNumber { get; set; }

        /// <summary>
        /// 关联项目名称
        /// </summary>
        public string? RelatedProjectName { get; set; }

        /// <summary>
        /// 计划状态
        /// </summary>
        public int PlanStatus { get; set; }

        /// <summary>
        /// 计划状态名称
        /// </summary>
        public string PlanStatusName => GetPlanStatusName(PlanStatus);

        /// <summary>
        /// 预计开始日期
        /// </summary>
        public DateTime? ExpectedStartDate { get; set; }

        /// <summary>
        /// 预计完成日期
        /// </summary>
        public DateTime? ExpectedEndDate { get; set; }

        /// <summary>
        /// 生产线
        /// </summary>
        public string? ProductionLine { get; set; }

        /// <summary>
        /// 优先级
        /// </summary>
        public int Priority { get; set; }

        /// <summary>
        /// 优先级名称
        /// </summary>
        public string PriorityName => GetPriorityName(Priority);

        /// <summary>
        /// 创建人
        /// </summary>
        public string? CreateName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        public DateTime? CreateDate { get; set; }

        /// <summary>
        /// 更新人
        /// </summary>
        public string? UpdateName { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        public DateTime? UpdateDate { get; set; }

        /// <summary>
        /// 生产物品列表
        /// </summary>
        public List<ProductionPlanItemDTO> ProductionItems { get; set; } = new List<ProductionPlanItemDTO>();

        /// <summary>
        /// 附件列表
        /// </summary>
        public List<ProductionPlanAttachmentDTO> Attachments { get; set; } = new List<ProductionPlanAttachmentDTO>();

        /// <summary>
        /// 获取计划状态名称
        /// </summary>
        private string GetPlanStatusName(int status)
        {
            return status switch
            {
                0 => "草稿",
                1 => "已确认",
                2 => "执行中",
                3 => "已完成",
                4 => "已取消",
                _ => "未知状态"
            };
        }

        /// <summary>
        /// 获取优先级名称
        /// </summary>
        private string GetPriorityName(int priority)
        {
            return priority switch
            {
                1 => "低",
                2 => "中",
                3 => "高",
                4 => "紧急",
                _ => "未知优先级"
            };
        }
    }

    /// <summary>
    /// 生产计划物品DTO
    /// </summary>
    public class ProductionPlanItemDTO
    {
        /// <summary>
        /// 生产计划物品ID
        /// </summary>
        public int PlanItemId { get; set; }

        /// <summary>
        /// 生产计划ID
        /// </summary>
        public int PlanId { get; set; }

        /// <summary>
        /// 物品编号
        /// </summary>
        public string ItemNumber { get; set; }

        /// <summary>
        /// 物品名称
        /// </summary>
        public string ItemName { get; set; }

        /// <summary>
        /// 规格型号
        /// </summary>
        public string? SpecificationModel { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string? Unit { get; set; }

        /// <summary>
        /// 物品类别
        /// </summary>
        public string? ItemCategory { get; set; }

        /// <summary>
        /// 物品分类
        /// </summary>
        public string? ItemClassification { get; set; }

        /// <summary>
        /// 品牌
        /// </summary>
        public string? Brand { get; set; }

        /// <summary>
        /// 数量
        /// </summary>
        public int Quantity { get; set; }

        /// <summary>
        /// 计划数量
        /// </summary>
        public int PlannedQuantity { get; set; }

        /// <summary>
        /// 完成数量
        /// </summary>
        public int CompletedQuantity { get; set; }

        /// <summary>
        /// 完成率
        /// </summary>
        public decimal CompletionRate => PlannedQuantity > 0 ? Math.Round((decimal)CompletedQuantity / PlannedQuantity * 100, 2) : 0;

        /// <summary>
        /// 排序序号
        /// </summary>
        public int SortOrder { get; set; }
    }

    /// <summary>
    /// 生产计划附件DTO
    /// </summary>
    public class ProductionPlanAttachmentDTO
    {
        /// <summary>
        /// 附件ID
        /// </summary>
        public int AttachmentId { get; set; }

        /// <summary>
        /// 生产计划ID
        /// </summary>
        public int PlanId { get; set; }

        /// <summary>
        /// 文件名
        /// </summary>
        public string FileName { get; set; }

        /// <summary>
        /// 文件大小（字节）
        /// </summary>
        public long FileSize { get; set; }

        /// <summary>
        /// 文件大小（可读格式）
        /// </summary>
        public string FileSizeText => GetFileSizeText(FileSize);

        /// <summary>
        /// 文件路径
        /// </summary>
        public string FilePath { get; set; }

        /// <summary>
        /// 文件类型
        /// </summary>
        public string? FileType { get; set; }

        /// <summary>
        /// 上传时间
        /// </summary>
        public DateTime? UploadDate { get; set; }

        /// <summary>
        /// 上传人
        /// </summary>
        public string? UploadBy { get; set; }

        /// <summary>
        /// 获取文件大小文本
        /// </summary>
        private string GetFileSizeText(long bytes)
        {
            if (bytes < 1024) return $"{bytes} B";
            if (bytes < 1024 * 1024) return $"{Math.Round((double)bytes / 1024, 2)} KB";
            if (bytes < 1024 * 1024 * 1024) return $"{Math.Round((double)bytes / (1024 * 1024), 2)} MB";
            return $"{Math.Round((double)bytes / (1024 * 1024 * 1024), 2)} GB";
        }
    }
} 